var prefix = jsctx + "/member/multchargeApply";

var memberMultchargeApply = (function () {
    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
        load();
    };

    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
    };
    var h = $(window).height() - 80;

    function load() {
        $('#exampleTable')
            .bootstrapTable({
                method: 'get',
                url: prefix + "/listMemberMultchargeApply",
                iconSize: 'outline',
                toolbar: '#exampleToolbar',
                striped: false,
                dataType: "json",
                pagination: true,
                singleSelect: false,
                pageSize: 20,
                pageList: [10, 20, 30],
                pageNumber: 1,
                sidePagination: "server",
                height: h,
                paginationShowPageGo: true,
                queryParams: function (params) {
                    return {
                        limit: params.limit,
                        offset: (params.offset / params.limit) + 1,
                        mobile: $("#mobile").val().trim(),
                        status: $("#status").val().trim()
                    };
                },
                columns: [
                    {
                        field: 'number',
                        title: '序号',
                        width: '50', align: 'center',
                        formatter: function (value, row, index) {
                            return index + 1;
                        }
                    },

                    {field: 'nickname', title: '昵称', align: 'center', width: '100'},
                    {field: 'mobile', title: '手机号', align: 'center', width: '100'},
                    {field: 'createdTime', title: '申请时间', align: 'center', width: '100'},
                    {
                        field: 'applyType',
                        title: '审核类型',
                        width: '100',
                        align: 'center',
                        formatter: function (value, row, index) {
                            if (row.applyType == '0') {
                                return '<span class="label label-success">申请多充</span>';
                            } else if (row.applyType == '1') {
                                return '<span class="label label-warning">取消多充</span>';
                            }
                        }
                    },
                    {field: 'reason', title: '申请原因', align: 'center', width: '100'},
                    {field: 'auditingTime', title: '审核时间', align: 'center', width: '100'},
                    {
                        field: 'status',
                        title: '状态',
                        width: '100',
                        align: 'center',
                        formatter: function (value, row, index) {
                            if (row.status == '0') {
                                return '<span class="label label-warning">未审核</span>';
                            } else if (row.status == '1') {
                                return '<span class="label label-primary">审核通过</span>';
                            } else if (row.status == '2') {
                                return '<span class="label label-danger">审核不通过</span>';
                            }
                        }
                    },
                    {
                        title: '操作',
                        field: 'applyId',
                        width: '200', align: 'center',
                        formatter: function (value, row, index) {
                            var a = '';
                            var b = '';
                            if (MemberManager_multchargeApply_update) {
                                a = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="通过" onclick="memberMultchargeApply.approval(\''
                                    + row.applyId + '\',' + '\'' + row.mobile + '\',' + '\'' + 1
                                    + '\')">通过</a> ';
                                b = '<a class="btn btn-danger btn-sm ' + has_edit + '" href="#" title="不通过" onclick="memberMultchargeApply.approval(\''
                                    + row.applyId + '\',' + '\'' + row.mobile + '\',' + '\'' + 2
                                    + '\')">不通过</a> ';
                            }
                            if (row.status == 0) {
                                return a + b;
                            }
                        }
                    }
                ]
            });
    };

    function approval(applyId, name, status) {
        layer.confirm('确定核准?' + name, {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/approval",
                type: "post",
                data: {
                    'applyId': applyId,
                    'status': status
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("操作成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    };

    function cancelApproval(applyId, name) {
        layer.confirm('确定取消?' + name, {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/cancelApproval",
                type: "post",
                data: {
                    'applyId': applyId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("操作成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    };

    return {
        init: init,
        reLoad: reLoad,
        approval: approval,
        cancelApproval: cancelApproval
    };
})();

